<template>
	<view class="content">
		<view class="list-box"v-if="itemList.length>0">
			<view class="yy-inquiry-user-info" v-for="(item,index) in itemList">
				<view class="user-left">
					<view class="icon">
						<image v-if="item.avatar" :src="item.avatar" mode="aspectFill">
						</image>
						<image v-else src="@/static/images/f.png" mode="aspectFill"></image>
					</view>
				</view>
				<view class="user-right">
					<view class="info">
						{{ infoFormat(item) }}
					</view>
					<view class="detail">
						{{ bodyStatae(item)}}
					</view>
				</view>
				
				<view class="user-ctr">
					<view class="ctr-item" @click="clickEdit(item)">编辑</view>
					<view class="ctr-item del" @click="clickDel(item)">删除</view>
				</view>
				
			</view>
			<!-- <view class='loadingicon acea-row row-center-wrapper' @click="clickMore">
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
			</view> -->
		</view>
		<view class="nodata" v-else>
			<emptyPage title="暂无数据～"></emptyPage>
		</view>
	</view>
</template>

<script>
	import emptyPage from '@/components/emptyPage.vue';
	import {
		userinfoFormat,
		userBodyStataeFormat
	} from "@/utils/format.js";
	import {
		getPatientList,
		delPatient
	} from '@/api/inquiry.js';
	import {
		mapGetters
	} from "vuex";

	export default {
		components:{
			emptyPage
		},
		computed: mapGetters(['isLogin', 'viewColor', 'uid','routeError']),
		data() {
			return {
				loadTitle: '加载更多',
				loading: false,
				loadend: false,
				where: {
					// 'page': 1,
					// 'limit':20,
				},
				itemList: [],
			}
		},
		onLoad() {
			if(this.routeError) {
				this.$store.commit('GO_ERROR');
				return
			}
			this.getList();
			
			this.$nextTick(() => {
				let that = this;
				uni.$on('yy_patient_edit', () => {
					that.getList();
				});
			});
		},
		onPullDownRefresh() {
			console.log('rk==>refresh')
			// this.loadend = false;
			// this.where.page = 1;
			this.clickMore();
		},
		onReachBottom() {
			this.clickMore();
		},
		methods: {
			/**
			 * 信息格式化
			 */
			infoFormat(info) {
				return userinfoFormat(info);
			},
			/**
			 * 状态格式化
			 */
			bodyStatae(info) {
				return userBodyStataeFormat(info);
			},
			/**
			 * 列表
			 */
			getList() {
				let that = this;
				that.where.uid = this.uid;
				getPatientList(that.where).then(res => {
					uni.stopPullDownRefresh();

					that.itemList = [];
					let resList = res.data;
					that.itemList = that.$util.SplitArray(resList, that.itemList);
					that.$set(that, 'itemList', that.itemList);

				});

			},
			/**
			 * 更多
			 */
			clickMore() {
				if (this.loadend) {
					return
				}
				this.getList();
			},
			/**
			 * 删除-提示
			 */
			clickDel(item) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除？',
					success: function(res) {
						if (res.confirm) {
							console.log('删除')
							that.sureDel(item);
						} else if (res.cancel) {
							console.log('不删除')
						}
					}
				});
			},
			/**
			 * 确认删除
			 */
			sureDel(item){
				let where = {
					'patient_id':item.id,
				};
				delPatient(where).then(res => {
					if(res.status == 200){
						this.getList();
					}
					this.$util.Tips({
						title:res.message
					});
				});
			},
			/**
			 * 编辑
			 */
			clickEdit(item){
				let pJson = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url:'/pages/shops/subs_inquiry/addpatient/index?patient_info='+pJson,
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		.list-box {
			background-color: #fff;
			padding: 50rpx 30rpx 0;
		}
	}
</style>
